package com.hnqcgczy.shop.dao.impl;

import com.hnqcgczy.shop.dao.IProductDAO;
import com.hnqcgczy.shop.db.Mytransaction;
import com.hnqcgczy.shop.vo.TProduct;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements IProductDAO {
    private Mytransaction mytransaction;

    public ProductDaoImpl(Mytransaction mytransaction) {
        this.mytransaction = mytransaction;
    }

    @Override
    public List<TProduct> listAll() throws SQLException {
        Connection conn = mytransaction.getConnection();
        String sql="select  * from t_product ";
        PreparedStatement pr = conn.prepareStatement(sql);
        ResultSet rs = pr.executeQuery();
        List<TProduct> list= new ArrayList<>();
        while (rs.next()){
            TProduct tProduct=new TProduct();
            tProduct.setTpcId(rs.getInt(1));
            tProduct.setTpName(rs.getString(2));
            tProduct.setTpDescription(rs.getString(3));
            tProduct.setTpPrice(rs.getDouble(4));
            tProduct.setTpStock(rs.getInt(5));
            tProduct.setTpcId(rs.getInt(6));
            tProduct.setTpcChildId(rs.getInt(7));
            tProduct.setTpFileName(rs.getString(8));

            list.add(tProduct);
        }
        return list;
    }

    @Override
    public List<TProduct> queryAny(TProduct product, int page, int pageSize) throws  SQLException {
        Connection conn = mytransaction.getConnection();
        StringBuffer stringBuffer = new StringBuffer("select  * from  t_order where  1=1");
        if (!product.getTpName().equals("")){
            stringBuffer.append("and tp_name like ?");
        }
        if (!product.getTpDescription().equals("")){
            stringBuffer.append("and tp_description like ?");
        }
        stringBuffer.append("limit ?,?");
        PreparedStatement pr = conn.prepareStatement(stringBuffer.toString());
        pr.setString(1,"%"+product.getTpName()+"%");
        pr.setString(2,"%"+product.getTpDescription()+"%");
        pr.setInt(3,(page-1)*pageSize);
        pr.setInt(4,pageSize);
        ResultSet rs = pr.executeQuery();
        List<TProduct> list= new ArrayList<>();
        while (rs.next()){
            TProduct tProduct=new TProduct();
            tProduct.setTpcId(rs.getInt(1));
            tProduct.setTpName(rs.getString(2));
            tProduct.setTpDescription(rs.getString(3));
            tProduct.setTpPrice(rs.getDouble(4));
            tProduct.setTpStock(rs.getInt(5));
            tProduct.setTpcId(rs.getInt(6));
            tProduct.setTpcChildId(rs.getInt(7));
            tProduct.setTpFileName(rs.getString(8));
            list.add(tProduct);
        }
        rs.close();
        pr.close();
        mytransaction.closeconn();
        return list;
    }

    @Override
    public TProduct queryById(int tp_id) throws SQLException {
        Connection conn = mytransaction.getConnection();
        String sql="select  * from t_product where tp_id=?";
        PreparedStatement pr = conn.prepareStatement(sql);
        pr.setInt(1,tp_id);
        ResultSet rs = pr.executeQuery();
        TProduct tProduct = null;
        if (rs.next()){
            tProduct=new TProduct();
            tProduct.setTpcId(rs.getInt(1));
            tProduct.setTpName(rs.getString(2));
            tProduct.setTpDescription(rs.getString(3));
            tProduct.setTpPrice(rs.getDouble(4));
            tProduct.setTpStock(rs.getInt(5));
            tProduct.setTpcId(rs.getInt(6));
            tProduct.setTpcChildId(rs.getInt(7));
            tProduct.setTpFileName(rs.getString(8));
        }
        rs.close();
        pr.close();
        mytransaction.closeconn();

        return tProduct;
    }

    @Override
    public boolean deleteById(int tp_id) throws SQLException {
        return false;
    }

    @Override
    public boolean updateByid(int tp_id) throws SQLException {
        return false;
    }

    @Override
    public boolean insert(TProduct tProduct) throws SQLException {
        return false;
    }

    public static void main(String[] args) {
        ProductDaoImpl productDao = new ProductDaoImpl(new Mytransaction(true));
        TProduct t=new TProduct();
        t.setTpName("黄桃");
        t.setTpDescription("炎");
        try {
            List<TProduct>list=productDao.queryAny(t,2,3);
            System.out.println(list);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
